/**
 * Created by 15c on 2018/2/9.
 */
//用户购买记录
app.controller('boughtrecord', function ($scope, $http) {
    var token = localStorage.getItem('token');
    //判断有无证书
    if (!token) {
        $.toast('请先登录');
        location.href = '#user-login';
        return;
    }
    //展示购买记录
    $scope.showBuyClass = function () {
        var infiniteScrollPreloader = $('.infinite-scroll-preloader');
        $http({
            method: 'get',
            url: app.netPath + '/order/buyclass',
            headers: {Authorization: token},
        }).then(function (res) {
            // 删除加载提示符
            infiniteScrollPreloader.hide();
            console.log(res.data);
            if (!res.data.error) {
                if(res.data.msg=="查询结果为空"){
                    $scope.buyClassCount=0
                }
                else{
                    $scope.buyClassCount = res.data.data.length;

                }
                $scope.buyClass = res.data.data;

                for (var i = 0; i < $scope.buyClass.length; i++) {
                    if ($scope.buyClass[i].aClass.classImg) {
                        $scope.buyClass[i].aClass.classImg = app.netPath + '/resources/classImg/' + $scope.buyClass[i].aClass.classImg;
                        app.buildImageUrl($scope.buyClass[i].aClass,'classImg',function () {
                            $scope.$digest();
                        });
                    }
                }

            }
            else {
                $.toast(res.data.msg)
            }
        })
    };
    //删除购买记录
    $scope.deleteBuyClass = function (orderId) {
       $.confirm('是否确认删除？','',function(){
           $http({
               method: 'get',
               url: app.netPath + '/order/delete/buyclass',
               headers: {Authorization: token},
               params: {orderId: orderId}
           }).then(function (res) {
               if (!res.data.error) {
                   $.toast(res.data);
                   location.href ='#boughtrecord';
               }
               else {
                   $.toast(res.data.msg)
               }
           })
       },function(){
           $.toast('删除取消！')
       })
    };


    //跳转课程详情页面
    $scope.getClassInfo = function (classId) {
        sessionStorage.setItem('classId', classId);
        location.href = '#xiangqing';
    };
});